package com.xkdx.guangguang.fragment.user;

import java.util.HashMap;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.xkdx.caipiao.R;
import com.xkdx.guangguang.ManagerActivity;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.fragment.user.UserModifyPwdModule.ModifyUserPW;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;

public class UserModifypwFragment extends BaseFragment implements OnClickListener {

	private View view;
	private String userID, userToken;
	private Button back, finish;
	private EditText inputold, reinputpw, inputpw;
	private ProgressDialog modifyPwdLoading;

	private UserModifyPwdAction actions;
	private UserModifyPwdModule module;
	private UserModifyPwdPresenter presenter;
	private ModifyUserPW user;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.user_success_modifypwd, container, false);
		findViews();

		return view;
	}

	private void findViews() {
		back = (Button) view.findViewById(R.id.user_modifypwd_back);
		finish = (Button) view.findViewById(R.id.btn_modifyfinish);

		inputold = (EditText) view.findViewById(R.id.ed_inputold);
		reinputpw = (EditText) view.findViewById(R.id.ed_reinputpw);
		inputpw = (EditText) view.findViewById(R.id.ed_inputnewpw);

		inputold.setTransformationMethod(PasswordTransformationMethod.getInstance());
		reinputpw.setTransformationMethod(PasswordTransformationMethod.getInstance());
		inputpw.setTransformationMethod(PasswordTransformationMethod.getInstance());

		back.setOnClickListener(this);
		finish.setOnClickListener(this);

		UserSharePrefence sp = new UserSharePrefence(getActivity(), IConstants.SP_USER);
		userID = sp.getUserID();
		userToken = sp.getUserLoginToken();

		modifyPwdLoading = new ProgressDialog(getActivity());
		modifyPwdLoading.setMessage("正在修改密码,请耐心等待...");
		modifyPwdLoading.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		modifyPwdLoading.setCancelable(true);
		modifyPwdLoading.setCanceledOnTouchOutside(false);// 解决4.0以上系统触屏后执行onCanel方法
		modifyPwdLoading.setOnCancelListener(new DialogInterface.OnCancelListener() {
			@Override
			public void onCancel(DialogInterface dialog) {
				getActivity().finish();
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.user_modifypwd_back:
			FragmentManager fm = getFragmentManager();
			fm.popBackStack();
			break;

		case R.id.btn_modifyfinish:
			if (!CheckPwd()) {
				return;
			}

			shwoLoading();
			break;
		}
	}

	/**
	 * 显示进度条
	 */
	private void shwoLoading() {
		modifyPwdLoading.show();
		actions = new UserModifyPwdAction(userID, userToken, inputold.getText().toString().trim(), inputpw.getText()
				.toString().trim());
		module = new UserModifyPwdModule(getActivity());
		presenter = new UserModifyPwdPresenter(this);
		((ManagerActivity) getActivity()).addCurrentTask(presenter);
		presenter.setActitons(actions);
		presenter.setModule(module);
		presenter.execute();

	}

	/**
	 * 检查输入的密码
	 * 
	 * @return
	 */
	private boolean CheckPwd() {
		if (inputold.getText().toString().trim().equals("")) {
			Toast.makeText(getActivity(), "请输入旧密码...", Toast.LENGTH_SHORT).show();
			inputold.setFocusable(true);
			inputold.setFocusableInTouchMode(true);
			inputold.requestFocus();
			return false;
		} else if (inputpw.getText().toString().trim().equals("")) {
			Toast.makeText(getActivity(), "请输入新密码...", Toast.LENGTH_SHORT).show();
			inputpw.setFocusable(true);
			inputpw.setFocusableInTouchMode(true);
			inputpw.requestFocus();
			return false;
		} else if (reinputpw.getText().toString().trim().equals("")) {
			Toast.makeText(getActivity(), "请输入确认密码...", Toast.LENGTH_SHORT).show();
			reinputpw.setFocusable(true);
			reinputpw.setFocusableInTouchMode(true);
			reinputpw.requestFocus();
			return false;
		} else if (!(inputpw.getText().toString().trim().equals(reinputpw.getText().toString().trim()))) {
			Toast.makeText(getActivity(), "两次输入的密码不同，请检查...", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}

	public void showLoaded(HashMap<String, AbsModule> msg) {
		if (modifyPwdLoading != null && modifyPwdLoading.isShowing()) {
			modifyPwdLoading.dismiss();
		}

		if (msg.containsKey("0")) {
			user = ((UserModifyPwdModule) msg.get("0")).user;
			if (user != null && user.getStatus().equals("Success")) {
				Toast.makeText(getActivity(), "密码修改成功...", 0).show();
				FragmentManager fm = getFragmentManager();
				fm.popBackStack();

			} else {
				Toast.makeText(getActivity(), user.getMessage(), 0).show();
			}

		} else {
			showErrorTip(msg);
		}

	}

	@Override
	public void showClick(HashMap<String, AbsModule> map) {

	}

	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {

	}

	@Override
	public void showLoading() {
		// TODO Auto-generated method stub

	}

	@Override
	public void findView() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setOnClick() {
		// TODO Auto-generated method stub
		
	}

}
